package com.sakura.flowers.controller;


import com.sakura.flowers.entity.CategoryFlower;
import com.sakura.flowers.service.CategoryFlowerService;
import com.sakura.flowers.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (CategoryFlower)表控制层
 *
 * @author Sakura
 * @since 2023-11-26 17:11:07
 */
@Api(value = "/api/categoryFlower", tags = {"(CategoryFlower)表控制层"})
@RestController
@RequestMapping("/api/categoryFlower")
@CrossOrigin
public class CategoryFlowerController {
    /**
     * 服务对象
     */
    @Resource
    private CategoryFlowerService categoryFlowerService;

    /**
     * 查询全部
     */
    @GetMapping("/list")
    public R list() {
        return R.success(this.categoryFlowerService.list());
    }

    /**
     * 分页查询
     *
     * @param pageNo    当前页
     * @param pageSize  数据大小
     * @param searchKey 查询参数
     * @return 查询结果
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "pageNo", value = "当前页", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "pageSize", value = "数据大小", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "searchKey", value = "查询参数")
    })
    @ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "GET")
    @GetMapping("/{pageNo}/{pageSize}")
    public R queryByPage(@PathVariable Integer pageNo,
                                                 @PathVariable Integer pageSize,
                                                 @RequestParam(required = false) String searchKey) {
        return R.success(this.categoryFlowerService.queryByPage(pageNo, pageSize, searchKey));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", value = "主键", required = true)
    })
    @ApiOperation(value = "通过主键查询单条数据", notes = "通过主键查询单条数据", httpMethod = "GET")
    @GetMapping("{id}")
    public R<CategoryFlower> queryById(@PathVariable("id") Long id) {
        return R.success(this.categoryFlowerService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param categoryFlower 实体
     * @return 新增结果
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "CategoryFlower", name = "categoryFlower", value = "实体", required = true)
    })
    @ApiOperation(value = "新增数据", notes = "新增数据", httpMethod = "POST")
    @PostMapping
    public R add(@RequestBody CategoryFlower categoryFlower) {
        boolean result = this.categoryFlowerService.save(categoryFlower);
        if (!result) {
            return R.error("添加失败");
        }
        return R.success("添加成功");
    }

    /**
     * 编辑数据
     *
     * @param categoryFlower 实体
     * @return 编辑结果
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "CategoryFlower", name = "categoryFlower", value = "实体", required = true)
    })
    @ApiOperation(value = "编辑数据", notes = "编辑数据", httpMethod = "PUT")
    @PutMapping
    public R edit(@RequestBody CategoryFlower categoryFlower) {
        boolean result = this.categoryFlowerService.updateById(categoryFlower);
        if (!result) {
            return R.error("修改失败");
        }
        return R.success("修改成功");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", value = "主键", required = true)
    })
    @ApiOperation(value = "删除数据", notes = "删除数据", httpMethod = "DELETE")
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Long id) {
        boolean result = this.categoryFlowerService.removeById(id);
        if (!result) {
            return R.error("删除失败");
        }
        return R.success("删除成功");
    }
}
